home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.4 / AdaptingToFonts / RenderInfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  6.4 KB  |  255 lines

  1. /*
  2.  * MKSoft Development Amiga ToolKit V1.0
  3.  *
  4.  * Copyright (c) 1985,86,87,88,89,90 by MKSoft Development
  5.  *
  6.  * $Id: renderinfo.c,v 1.3.1 90/06/13 09:53:22 mks Exp Locker: mks $
  7.  *
  8.  * $Source: Programming:MKSoft/MKS/RCS/renderinfo.c,v $
  9.  *
  10.  * $Date: 90/06/13 09:53:22 $
  11.  *
  12.  * $Revision: 1.3.1 $
  13.  *
  14.  * $Log:    renderinfo.c,v $
  15.  * Revsison 1.3.1  90/06/13  09:53:22  mks
  16.  * Special version for DevCon examples...  This is a RCS branch
  17.  * and will not be part of the main tree...
  18.  *
  19.  * Revision 1.3  90/06/03  10:25:59  mks
  20.  * Fixed the fact the bottom value was set wrong!
  21.  *
  22.  * Revision 1.2  90/05/20  12:17:03  mks
  23.  * New functions to allocate and free RenderInfo structure.
  24.  * These should be used insted of the old FillIn_RenderInfo...
  25.  * Now has a TextAttr in the RenderInfo
  26.  * Now has the screen Width/Height in RenderInfo
  27.  * Now can be passed a screen pointer
  28.  *
  29.  * Revision 1.1  90/05/09  21:45:02  mks
  30.  * Source now fully under RCS...
  31.  *
  32.  */
  33.  
  34. /*
  35.  ************************************************************************
  36.  *                                                                      *
  37.  *                            DISCLAIMER                                *
  38.  *                                                                      *
  39.  *   THIS SOFTWARE IS PROVIDED "AS IS".                                 *
  40.  *   NO REPRESENTATIONS OR WARRANTIES ARE MADE WITH RESPECT TO THE      *
  41.  *   ACCURACY, RELIABILITY, PERFORMANCE, CURRENTNESS, OR OPERATION      *
  42.  *   OF THIS SOFTWARE, AND ALL USE IS AT YOUR OWN RISK.                 *
  43.  *   NEITHER COMMODORE NOR THE AUTHORS ASSUME ANY RESPONSIBILITY OR     *
  44.  *   LIABILITY WHATSOEVER WITH RESPECT TO YOUR USE OF THIS SOFTWARE.    *
  45.  *                                                                      *
  46.  ************************************************************************
  47.  */
  48.  
  49. /*
  50.  * This file contains the definition of the rendering information
  51.  * for elements on the screen.  This information is used to generate
  52.  * the correct pen colours for items on the screen...
  53.  */
  54.  
  55. #include    <exec/types.h>
  56. #include    <exec/memory.h>
  57. #include    <graphics/view.h>
  58. #include    <graphics/text.h>
  59. #include    <intuition/intuition.h>
  60. #include    <intuition/screens.h>
  61.  
  62. #include    <proto/exec.h>
  63. #include    <proto/intuition.h>
  64. #include    <proto/graphics.h>
  65.  
  66. #include    "renderInfo.h"
  67.  
  68. /*
  69.  * The "default" font we might need...
  70.  */
  71. static char fontnam[11]="topaz.font";
  72. static struct TextAttr TOPAZ80={fontnam,8,0,FPF_ROMFONT};
  73.  
  74. /*
  75.  * These define the amount of Red, Green, and Blue scaling used
  76.  * to help take into account the different visual impact of those
  77.  * colours on the screen.
  78.  */
  79. #define    BLUE_SCALE    2
  80. #define    GREEN_SCALE    6
  81. #define    RED_SCALE    3
  82.  
  83. /*
  84.  * This returns the colour difference hamming value...
  85.  */
  86. SHORT ColourDifference(UWORD rgb0, UWORD rgb1)
  87. {
  88. register    SHORT    level;
  89. register    SHORT    tmp;
  90.  
  91.     tmp=(rgb0 & 15) - (rgb1 & 15);
  92.     level=tmp*tmp*BLUE_SCALE;
  93.     tmp=((rgb0>>4) & 15) - ((rgb1>>4) & 15);
  94.     level+=tmp*tmp*GREEN_SCALE;
  95.     tmp=((rgb0>>8) & 15) - ((rgb1>>8) & 15);
  96.     level+=tmp*tmp*RED_SCALE;
  97.     return(level);
  98. }
  99.  
  100. /*
  101.  * Calculate a rough brightness hamming value...
  102.  */
  103. SHORT ColourLevel(UWORD rgb)
  104. {
  105.     return(ColourDifference(rgb,0));
  106. }
  107.  
  108. /*
  109.  * Define the maximum colours to look at.
  110.  * This is my way of making this work "everywhere" including HAM
  111.  */
  112. #define    MAX_COLOURS    16
  113.  
  114. /*
  115.  * For new programs, this also opens fonts...
  116.  */
  117. static VOID NewFillIn_RenderInfo(struct RenderInfo *ri,struct Screen *TheScreen)
  118. {
  119. register    SHORT        numcolours;
  120. register    SHORT        loop;
  121. register    SHORT        loop1;
  122. register    SHORT        backpen;
  123. register    SHORT        tmp;
  124.         SHORT        colours[16];
  125.         SHORT        colourlevels[16];
  126.         SHORT        pens[16];
  127.     struct    Screen        screen;
  128.  
  129.     /*
  130.      * If no screen was passed we will used the old (1.2 compatible)
  131.      * way of getting at the screen...
  132.      */
  133.     if (!TheScreen) GetScreenData((CPTR)(TheScreen=&screen),sizeof(struct Screen),WBENCHSCREEN,NULL);
  134.  
  135.     ri->WindowTop=TheScreen->WBorTop;
  136.     ri->WindowLeft=TheScreen->WBorLeft;
  137.     ri->WindowRight=TheScreen->WBorRight;
  138.     ri->WindowBottom=TheScreen->WBorBottom;
  139.     ri->WindowTitle=TheScreen->BarHeight-TheScreen->BarVBorder+TheScreen->WBorTop;
  140.  
  141.     ri->ScreenWidth=TheScreen->Width;
  142.     ri->ScreenHeight=TheScreen->Height;
  143.  
  144.     /*
  145.      * If we can't open the font given for the screen we need
  146.      * to fall-back to Topaz...  *YUCK!*
  147.      */
  148.     Forbid();
  149.     if (TheScreen->Font) if (!(ri->TheFont=OpenFont(TheScreen->Font))) ri->TheFont=OpenFont(&TOPAZ80);
  150.     Permit();
  151.  
  152.     if (ri->TheFont)
  153.     {
  154.         ri->TextAttr.ta_Name=ri->TheFont->tf_Message.mn_Node.ln_Name;
  155.         ri->TextAttr.ta_YSize=ri->TheFont->tf_YSize;
  156.         ri->TextAttr.ta_Style=ri->TheFont->tf_Style;
  157.         ri->TextAttr.ta_Flags=ri->TheFont->tf_Flags;
  158.     }
  159.     else ri->TextAttr=TOPAZ80;
  160.  
  161.     ri->FontSize=ri->TextAttr.ta_YSize;
  162.  
  163.     numcolours=1 << (TheScreen->RastPort.BitMap->Depth);
  164.     if (numcolours>16) numcolours=16;
  165.  
  166.     if (numcolours<3)
  167.     {    /* Some silly person is running with 2 colours... */
  168.         ri->BackPen=0;
  169.         ri->Highlight=1;
  170.         ri->Shadow=1;
  171.         ri->TextPen=1;
  172.     }
  173.     else
  174.     {
  175.         Forbid();
  176.         for (loop=0;loop<numcolours;loop++)
  177.         {
  178.             colours[loop]=GetRGB4(TheScreen->ViewPort.ColorMap,(LONG)loop);
  179.             colourlevels[loop]=ColourLevel(colours[loop]);
  180.             pens[loop]=loop;
  181.         }
  182.         Permit();
  183.  
  184.         /* Sort darkest to brightest... */
  185.         for (loop=0;loop<(numcolours-1);loop++)
  186.          for (loop1=loop+1;loop1<numcolours;loop1++)
  187.          {
  188.             if (colourlevels[loop]>colourlevels[loop1])
  189.             {
  190.                 tmp=colourlevels[loop];
  191.                 colourlevels[loop]=colourlevels[loop1];
  192.                 colourlevels[loop1]=tmp;
  193.  
  194.                 tmp=colours[loop];
  195.                 colours[loop]=colours[loop1];
  196.                 colours[loop1]=tmp;
  197.  
  198.                 tmp=pens[loop];
  199.                 pens[loop]=pens[loop1];
  200.                 pens[loop1]=tmp;
  201.             }
  202.          }
  203.  
  204.         /* Now, pick the pens... HightLight... */
  205.         loop=numcolours-1;
  206.         while (!(ri->Highlight=pens[loop--]));
  207.  
  208.         /* and Shadow... */
  209.         loop=0;
  210.         while (!(ri->Shadow=pens[loop++]));
  211.  
  212.         /* The BackGround pen... */
  213.         if (!pens[loop]) loop++;
  214.         ri->BackPen=pens[backpen=loop];
  215.  
  216.         loop1=0;
  217.         for (loop=0;loop<numcolours;loop++)
  218.         {
  219.             tmp=ColourDifference(colours[loop],colours[backpen]);
  220.             if (tmp>loop1)
  221.             {
  222.                 loop1=tmp;
  223.                 ri->TextPen=pens[loop];
  224.             }
  225.         }
  226.     }
  227. }
  228.  
  229. /*
  230.  * Close the font and free the memory...
  231.  */
  232. VOID CleanUp_RenderInfo(struct RenderInfo *ri)
  233. {
  234.     if (ri)
  235.     {
  236.         if (ri->TheFont) CloseFont(ri->TheFont);
  237.         FreeMem(ri,sizeof(struct RenderInfo));
  238.     }
  239. }
  240.  
  241. /*
  242.  * Use this screen for the render information.
  243.  */
  244. struct RenderInfo *Get_RenderInfo(struct Screen *TheScreen)
  245. {
  246. register    struct    RenderInfo    *ri;
  247.  
  248.     if (ri=AllocMem(sizeof(struct RenderInfo),MEMF_PUBLIC|MEMF_CLEAR))
  249.     {
  250.         NewFillIn_RenderInfo(ri,TheScreen);
  251.     }
  252.     return (ri);
  253. }
  254.  
  255.